package com.example.demo_one

import android.os.Bundle
import android.widget.SimpleAdapter
import android.widget.Spinner
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity

class CityIconActivity : AppCompatActivity() {

    // 1. 选数据
    data class CityData(val name: String, val icon: Int)

    val cityList = listOf(
        CityData("上海", R.drawable.sh),
        CityData("北京", R.drawable.bj),
        CityData("广州", R.drawable.gz),
        CityData("深圳", R.drawable.sz),
        CityData("杭州", R.drawable.hz),
        CityData("南京", R.drawable.nj),
    )

    //    private val spinner: Spinner? = null
    private lateinit var spinner: Spinner
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_city_icon)
        // 1. 获取下拉框组件
        spinner = findViewById<Spinner>(R.id.spinner_icon)
        // 2. 创建适配器
        val adapter = SimpleAdapter(
            this,
            cityList.map {
                mapOf("name" to it.name, "icon" to it.icon)
            },
            R.layout.item_icon,
            arrayOf("name", "icon"),
            intArrayOf(R.id.text, R.id.icon)
        )
        // 3. 设置适配器
        spinner.adapter = adapter
    }
}